//
//  CSHomeViewController.m
//  ChinaShip
//
//  Created by Yale on 2019/1/24.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSHomeViewController.h"
#import <TYCyclePagerView/TYCyclePagerView.h>
#import "CSHomeSlideCollectionViewCell.h"
#import "CSHomeBottomView.h"
#import "CSSectionListViewController.h"
#import "CSSwitchApi.h"
#import "CSSectionDetailViewController.h"
@interface CSHomeViewController () <TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>
@property (nonatomic, strong) UIImageView      *bgImgView;
@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) CSHomeBottomView *homeBottomView;
@property (nonatomic, strong) NSArray          *showImages,*showBgImgs,*titles;
@property (nonatomic, strong) UIView           *coverView;
@end

@implementation CSHomeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self hiddenNavBar];
    [self updateSwitch];
    [SVProgressHUD dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self addBgImgView];
    [self addPageView];
    [self addBottomView];
}

- (void)addBottomView{
    self.homeBottomView = [CSHomeBottomView createHomeBottomViewWithFrame:CGRectMake(0, kSCREEN_HEIGHT-200, kSCREEN_WIDTH, 200) imgList:self.showImages];
    [self.view addSubview:_homeBottomView];
}

- (void)addBgImgView{
    self.bgImgView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    self.bgImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.bgImgView.clipsToBounds = YES;
    self.bgImgView.image = [UIImage imageNamed:self.showBgImgs.firstObject];
    [self.view addSubview:self.bgImgView];
}

- (NSArray *)showImages{
    if (!_showImages) {
        _showImages = @[@"home_1",@"home_2",@"home_3",@"home_4",@"home_5"];
    }
    return _showImages;
}

- (NSArray *)showBgImgs{
    if (!_showBgImgs) {
        _showBgImgs = @[@"home_1_bg",@"home_2_bg",@"home_3_bg",@"home_4_bg",@"home_5_bg"];
    }
    return _showBgImgs;
}


- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@"CSSC in General",@"Capability Design",@"Naval ship and \n unmanned product",@"Systems and Equipments",@"Technological Innovation"];
    }
    return _titles;
}

- (void)addPageView{
    TYCyclePagerView *pageView = [TYCyclePagerView new];
    pageView.backgroundColor = [UIColor clearColor];
    pageView.isInfiniteLoop = YES;
    pageView.dataSource = self;
    pageView.delegate = self;
    [pageView registerNib:[UINib nibWithNibName:@"CSHomeSlideCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"homeSlide_cell"];
    [self.view addSubview:pageView];
    _pagerView = pageView;
    
    self.coverView = [UIView new];
    self.coverView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.coverView];
    
    UISwipeGestureRecognizer *swipLeftGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureAction:)];
    [swipLeftGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.coverView addGestureRecognizer:swipLeftGestureRecognizer];
   
    
    UISwipeGestureRecognizer *swipRightGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureAction:)];
    [swipRightGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.coverView addGestureRecognizer:swipRightGestureRecognizer];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
    [self.coverView addGestureRecognizer:tapGesture];
}

//单击
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)tap{
    CGSize itemSize = CGSizeZero;
    if (isIPhonex) {
        itemSize = CGSizeMake(CGRectGetWidth(_pagerView.frame)*0.5, 310);
    }else{
        itemSize = CGSizeMake(CGRectGetWidth(_pagerView.frame)*0.5, CGRectGetHeight(_pagerView.frame)*0.58);
    }
    CGRect jRect = CGRectMake((kSCREEN_WIDTH-itemSize.width)/2.0, (kSCREEN_HEIGHT-itemSize.height-150)/2, itemSize.width, itemSize.height);
    if (CGRectContainsPoint(jRect, [tap locationInView:tap.view])) {
        if (self.pagerView.curIndex == 0) {
            CSSectionDetailViewController *sectionDetailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CSSectionDetailViewController"];
            sectionDetailVC.mid = 9;
            [self pushVcToOtherVc:sectionDetailVC];
        }else{
            CSSectionListViewController *sectionListVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CSSectionListViewController"];
            if (self.pagerView.curIndex == 2) {
                sectionListVC.navigationItem.title = @"Naval ship and unmanned product";
            }else{
                sectionListVC.navigationItem.title = self.titles[self.pagerView.curIndex];
            }
            sectionListVC.mid = self.pagerView.curIndex;
            [self pushVcToOtherVc:sectionListVC];
        }
    }
}


- (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipeGesture{
    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
        [_pagerView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionRight animate:YES];
    }else{
        [_pagerView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionLeft animate:YES];
    }
}


- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    _pagerView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-150);
    self.coverView.frame = _pagerView.frame;
}

#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.showImages.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    CSHomeSlideCollectionViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"homeSlide_cell" forIndex:index];
    cell.layer.cornerRadius = 15.0f;
    [cell setCoverImage:self.showImages[index] title:self.titles[index]];
    return cell;
}

//点击跳转界面
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index{
    [self pushVcToOtherVc:[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CSSectionListViewController"]];
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    if (isIPhonex) {
        layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)*0.5, 310);
    }else{
        layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)*0.5, CGRectGetHeight(pageView.frame)*0.58);
    }
    layout.itemSpacing = kSCREEN_WIDTH*0.1;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    [self.homeBottomView scrollowFromIndex:fromIndex toIndex:toIndex];
    self.bgImgView.image = [UIImage imageNamed:self.showBgImgs[toIndex]];
}

- (void)updateSwitch{
    if ([CSTool isLogin]) {
        return;
    }
    CSSwitchApi *switchApi = [CSSwitchApi new];
    [switchApi loadRequestComplete:^(TBURLResponse *response, NSError *error) {
        CSResponse *_response = response.returnObject;
        if ([_response.status integerValue] == 0) {
            [CSTool saveSwitchState:_response.data];
        }else{
            [CSTool saveSwitchState:NO];
        }
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
